package co.fitstart.entity.coach;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.course.CourseType;
import co.fitstart.entity.course.CourseUnit;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

@Entity
@Table(name = "coach_signin_record")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CoachSigninRecord {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 上课教练 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Principal principal;
    
    /** 上课门店 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Site site;
    
    /** 
     * course round code + courseTime + courseDay  
     */
    private String code;
    
    /** 月份 */
    private String month;
    
    /** 课时数 */
    private BigDecimal hour;
    
    /** 课程描述（具体课程名称） */
    private String courseDescription;
    
    /** 课程🎧累呗（具体课程名称） */
    private String courseUnit;
    
    /** 上课日期 */
    @Type(type = "date")
    private Date courseDay;
    
    /** 上课时间 */
    private String courseTime;
    
    /** 创建时间 */
    private Date createTime;
    
    private String signPerson;
    
    /** 计算课时时，是否忽略 */
    @Column(name = "is_ignored")
    public boolean ignored;
    
    /**
     * 
     */
    public CoachSigninRecord() {
        
    }
    
    /**
     * 
     * @param courseSchedule
     */
    public CoachSigninRecord(CourseSchedule courseSchedule, String signPerson) {
        this.principal = courseSchedule.getCoach();
        this.site = courseSchedule.getSite();
        this.code = courseSchedule.getCourseRound().getCode();
        this.month = DateFormatUtils.format(courseSchedule.getOccurDate(), "yyyy-MM");
        this.hour = new BigDecimal(1);
        this.courseDescription = courseSchedule.getCourse().getCategoryName();
        this.courseUnit = courseSchedule.getCourse().getCourseUnit().getDescription();
        this.courseDay = courseSchedule.getOccurDate();
        this.courseTime = courseSchedule.getOccurHour();
        
        if(courseSchedule.getCourse().getCourseType() != null && courseSchedule.getCourse().getCourseType().equals(CourseType.POSTURE)) {
            this.hour = new BigDecimal(2);
        } else if(courseSchedule.getCourse().getCourseUnit() != null && courseSchedule.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_EXPERIENCE)) {
            this.hour = new BigDecimal(1.5);
        } else if(courseSchedule.getCourse().getCourseUnit() != null && (courseSchedule.getCourse().getCourseUnit().equals(CourseUnit.SPECIAL_UNION))) {
            this.hour = new BigDecimal(1.5);
        }
    	
    	this.signPerson = signPerson;
    	
        this.createTime = new Date();
        if(courseSchedule.getCourse().isExperience() && courseSchedule.getCourse().getCourseUnit() != null && courseSchedule.getCourse().getCourseUnit().equals(CourseUnit.NORMAL_EXPERIENCE) && !courseSchedule.getStudent().getUser().isOrdered()) {
            this.ignored = true;
        }
    }
    
    /**
     * 
     * @return
     */
    public CoachSigninRecord editIgnored() {
        this.ignored = false;
        return this;
    }

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the principal
	 */
	public Principal getPrincipal() {
		return principal;
	}

	/**
	 * @return the site
	 */
	public Site getSite() {
		return site;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the month
	 */
	public String getMonth() {
		return month;
	}

	/**
	 * @return the hour
	 */
	public BigDecimal getHour() {
		return hour;
	}

	/**
	 * @return the courseDescription
	 */
	public String getCourseDescription() {
		return courseDescription;
	}

	/**
	 * @return the courseUnit
	 */
	public String getCourseUnit() {
		return courseUnit;
	}

	/**
	 * @return the courseDay
	 */
	public Date getCourseDay() {
		return courseDay;
	}

	/**
	 * @return the courseTime
	 */
	public String getCourseTime() {
		return courseTime;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @return the ignored
	 */
	public boolean isIgnored() {
		return ignored;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param principal the principal to set
	 */
	public void setPrincipal(Principal principal) {
		this.principal = principal;
	}

	/**
	 * @param site the site to set
	 */
	public void setSite(Site site) {
		this.site = site;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param month the month to set
	 */
	public void setMonth(String month) {
		this.month = month;
	}

	/**
	 * @param hour the hour to set
	 */
	public void setHour(BigDecimal hour) {
		this.hour = hour;
	}

	/**
	 * @param courseDescription the courseDescription to set
	 */
	public void setCourseDescription(String courseDescription) {
		this.courseDescription = courseDescription;
	}

	/**
	 * @param courseUnit the courseUnit to set
	 */
	public void setCourseUnit(String courseUnit) {
		this.courseUnit = courseUnit;
	}

	/**
	 * @param courseDay the courseDay to set
	 */
	public void setCourseDay(Date courseDay) {
		this.courseDay = courseDay;
	}

	/**
	 * @param courseTime the courseTime to set
	 */
	public void setCourseTime(String courseTime) {
		this.courseTime = courseTime;
	}

	/**
	 * @param createTime the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * @param ignored the ignored to set
	 */
	public void setIgnored(boolean ignored) {
		this.ignored = ignored;
	}
	
	public String getSignPerson() {
		return signPerson;
	}
	public void setSignPerson(String signPerson) {
		this.signPerson = signPerson;
	}
}
